<template>
  <div class="home-container">
    <van-nav-bar fixed>
      <van-button slot="title" round type="info" size="small" icon="search" class="search-btn" to="/search">搜索</van-button>
    </van-nav-bar>
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab :title="channel.name" v-for="channel in channels" :key="channel.id">
        <articalList :channel="channel"></articalList>
      </van-tab>

      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="isChannelIdetShow = true">
        <i class="iconfont icon-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 展示弹出层 -->
    <van-popup round v-model="isChannelIdetShow" closeable position="bottom" :style="{ height: '95%' }" close-icon-position="top-left">
      <ChannelEdit :my-channels="channels" :active="active" @update-active="onUpdateActive"></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannel } from '@/api/users.js'
import articalList from '@/views/home/components/article-list.vue'
import ChannelEdit from '@/views/home/components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'
export default {
  name: 'HomeIndex',
  data() {
    return {
      active: 0,
      channels: [], // 频道列表
      isChannelIdetShow: false
    }
  },
  created() {
    this.loadChannels()
  },
  methods: {
    async loadChannels() {
      try {
        // const { data } = await getUserChannel()
        // // console.log(data)
        // this.channels = data.data.channels
        let channels = []
        if (this.user) {
          // 已登录 请求获取用户频道列表
          const { data } = await getUserChannel()
          channels = data.data.channels
        } else {
          // 未登录 判断是否有本地存储
          const localChannels = getItem('TOUTIAO_CHANNELS')
          // 如果有  就拿来用
          channels = localChannels
          //  如果没有  则获取默认频道列表
          const { data } = await getUserChannel()
          channels = data.data.channels
        }
        this.channels = channels
      } catch (err) {
        this.$toast('获取数据失败')
      }
    },
    onUpdateActive(index, isChannelIdetShow = true) {
      this.active = index
      this.isChannelIdetShow = isChannelIdetShow
    }
  },
  components: {
    articalList,
    ChannelEdit
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  background-color: #3296fa;
  /deep/.van-icon {
    color: #fff;
  }
}
/deep/.van-nav-bar__title {
  color: #fff;
}
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5ba6fc;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
    }
    .van-tab {
      min-width: 200px;
      // height: 82px;
      font-size: 30px;
      border: 1px solid #edeff3;
      color: #777;
    }
    .van-tab--active {
      color: #333;
    }
    .van-tabs__nav--line {
      padding-bottom: 0px;
    }
    .van-tabs__nav--complete {
      padding-left: 0;
    }
    .van-tabs__line {
      background-color: #3296fa;
      width: 31px;
      height: 6px;
      bottom: 8px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      background-color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      opacity: 0.92;
      i.iconfont {
        font-size: 33px;
      }
      &:before {
        content: '';
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url('../../assets/gradient-gray-line.png');
        background-size: contain;
      }
    }
    .placeholder {
      width: 66px;
      height: 82px;
      flex-shrink: 0;
    }
  }
}
</style>
